package cn.witsky.rtp2ws.domain;

import cn.witsky.rtp2ws.cache.UdpServerManager;
import cn.witsky.rtp2ws.util.SystemClock;
import net.sf.json.JSONObject;

public class BaseCall {
	//会话id
	private String callId;
	//主叫
	private String caller;
	//被叫
	private String called;
	//主叫ws推送地址
	private String callerWsUrl;
	//被叫ws推送地址
	private String calledWsUrl;
	//绑定的udp端口
	private Integer udpPort;
	private String srcHost="";
	private volatile Integer callerPort=-1;
	private volatile Integer calledPort=-1;
	private String x;
	private String asaId;
	private Integer callType=1;
	private String connectTm="";
	private String offhkTm="";
	private Integer callMode=0;
	private String user_data="";
	private String user_data_key="";
	private String user_extdata_key="";	
	private long createTime = SystemClock.now();
	private String rtp2wsHost="";
	private String recordUrl="";
	
	private boolean isBindSrcPort=false;

	public BaseCall(String callId) {
		this.callId = callId;
	}

	public String getCallId() {
		return callId;
	}

	public String getCaller() {
		return caller;
	}

	public void setCaller(String caller) {
		this.caller = caller;
	}

	public String getCalled() {
		return called;
	}

	public void setCalled(String called) {
		this.called = called;
	}

	public String getCallerWsUrl() {
		return callerWsUrl;
	}

	public void setCallerWsUrl(String callerWsUrl) {
		this.callerWsUrl = callerWsUrl;
	}

	public String getCalledWsUrl() {
		return calledWsUrl;
	}

	public void setCalledWsUrl(String calledWsUrl) {
		this.calledWsUrl = calledWsUrl;
	}

	public Integer getUdpPort() {
		return udpPort;
	}

	public void setUdpPort(Integer udpPort) {
		this.udpPort = udpPort;
	}

	public Integer getCallerPort() {
		return callerPort;
	}

	public void setCallerPort(Integer callerPort) {
		if (this.callerPort<0) {
				this.callerPort = callerPort;
		}
	}

	public Integer getCalledPort() {
		return calledPort;
	}

	public void setCalledPort(Integer calledPort) {
		if (this.calledPort<0) {
			this.calledPort = calledPort;
		}
	}
	public String getX() {
		return x;
	}

	public void setX(String x) {
		this.x = x;
	}

	public String getAsaId() {
		return asaId;
	}

	public void setAsaId(String asaId) {
		this.asaId = asaId;
	}

	public Integer getCallType() {
		return callType;
	}

	public void setCallType(Integer callType) {
		this.callType = callType;
	}

	public String getConnectTm() {
		return connectTm;
	}

	public void setConnectTm(String connectTm) {
		this.connectTm = connectTm;
	}

	public String getOffhkTm() {
		return offhkTm;
	}

	public void setOffhkTm(String offhkTm) {
		this.offhkTm = offhkTm;
	}

	public Integer getCallMode() {
		return callMode;
	}

	public void setCallMode(Integer callMode) {
		this.callMode = callMode;
	}

	public String getUser_data() {
		return user_data;
	}

	public void setUser_data(String user_data) {
		this.user_data = user_data;
	}

	public String getUser_data_key() {
		return user_data_key;
	}

	public void setUser_data_key(String user_data_key) {
		this.user_data_key = user_data_key;
	}

	public String getUser_extdata_key() {
		return user_extdata_key;
	}

	public void setUser_extdata_key(String user_extdata_key) {
		this.user_extdata_key = user_extdata_key;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public String toJsonString() {
		JSONObject json =JSONObject.fromObject(this);
		return json.toString();
	}

	public long getCreateTime() {
		return createTime;
	}

	
	public boolean isBindSrcPort() {
		return isBindSrcPort;
	}

	public void setBindSrcPort(boolean isBindSrcPort) {
		this.isBindSrcPort = isBindSrcPort;
	}

	
	public String getCallerSrc() {
		return srcHost+":"+this.getCallerPort();
	}
	
	public String getCalledSrc() {
		return srcHost+":"+this.getCalledPort();
	}

	public void setSrcHost(String srcHost) {
		this.srcHost = srcHost;
	}

	public void bindSrcPort(String srcHost,int srcPort,int dstPort) {
		this.setSrcHost(srcHost);
		if(this.udpPort==dstPort) {
			this.setCallerPort(srcPort);
		}else {
			this.setCalledPort(srcPort);
		}
		if(this.callerPort>0&&this.calledPort>0) {
			UdpServerManager.addSrcPortByCall(this);
		}
	}
	
	public void bindSrcPort() {
		if(this.callerPort>0&&this.calledPort>0) {
			UdpServerManager.addSrcPortByCall(this);
		}
	}
	
	
	public String getSrcHost() {
		return srcHost;
	}

	public void setCallId(String callId) {
		this.callId = callId;
	}


	public String getRtp2wsHost() {
		return rtp2wsHost;
	}

	public void setRtp2wsHost(String rtp2wsHost) {
		this.rtp2wsHost = rtp2wsHost;
	}

	public String getRecordUrl() {
		return recordUrl;
	}

	public void setRecordUrl(String recordUrl) {
		this.recordUrl = recordUrl;
	}

	@Override
	public String toString() {
		return "BaseCall [callId=" + callId + ", caller=" + caller + ", called=" + called + ", callerWsUrl="
				+ callerWsUrl + ", calledWsUrl=" + calledWsUrl + ", udpPort=" + udpPort + ", srcHost=" + srcHost
				+ ", callerPort=" + callerPort + ", calledPort=" + calledPort + ", x=" + x + ", asaId=" + asaId
				+ ", callType=" + callType + ", connectTm=" + connectTm + ", offhkTm=" + offhkTm + ", callMode="
				+ callMode + ", user_data=" + user_data + ", user_data_key=" + user_data_key + ", user_extdata_key="
				+ user_extdata_key + ", createTime=" + createTime + ", rtp2wsHost=" + rtp2wsHost + ", recordUrl="
				+ recordUrl + ", isBindSrcPort=" + isBindSrcPort + "]";
	} 
}
